<template>
  <!-- 走马灯 -->
  <div class="container">
    <el-carousel :interval="5000" arrow="always">
      <el-carousel-item v-for="(item,index) in banners" :key="index">
        <div
          class="banner-image"
          :style="`background:url(${item.url}) no-repeat center;
        background-size:contain contain`"
        ></div>
      </el-carousel-item>
    </el-carousel>

    <!-- 搜索框 -->
    <div class="banner-content">
      <div class="search-bar">
        <!-- tab栏 点击tab栏切换类名 -->
        <el-row type="flex" class="search-tab">
          <span
            v-for="(item,index) in options"
            :key="index"
            :class="{active:index==currentOption}"
            @click="handleTab(index)"
          >
            <i>{{item.name}}</i>
          </span>
        </el-row>

        <!-- 输入框 -->
        <el-row type="flex" align="middle" class="search-input">
          <!-- 输入框动态绑定占位符内容，双项数据绑定，按下回车键弹起触发搜索事件 -->
          <input
            type="text"
            :placeholder="options[currentOption].placeholder"
            v-model="searchValue"
            @keyup.enter="handleSearch"
          />
          <i class="el-icon-search" @click="handleSearch"></i>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  //数据
  data() {
    return {
      //轮播图
      banners: [],
      //当前选项
      currentOption: 0,
      //搜索框的value
      searchValue: "",
      //栏目数组
      options: [
        {
          name: "攻略",
          placeholder: "请输入城市搜索攻略",
          pageUrl: "/post?city="
        },
        {
          name: "酒店",
          placeholder: "请输入城市搜索酒店",
          pageUrl: "/hotel?city="
        },
        { name: "机票", placeholder: "请输入地点搜索机票", pageUrl: "/air" }
      ]
    };
  },
  //声明周期函数
  created() {
    this.$axios({
      url: "/scenics/banners"
    }).then(res => {
      console.log(res.data);
      //映射之后改变url的结构，拼接上基准路径
      this.banners = res.data.data.map(el => {
        return { ...el, url: this.$axios.defaults.baseURL + el.url };
      });
      console.log(this.banners);
    });
  },
  //方法
  methods: {
    //tab栏点击事件
    handleTab(index) {
      //currentOption:当前选项
      this.currentOption = index;
      //如果当前选项是机票就跳转到机票页面
      let item = this.options[this.currentOption];
      if (item.name == "机票") {
        return this.$router.push(item.pageUrl);
      }
    },
    //搜索框事件
    handleSearch() {
      //判断传入的输入框的值
      let item = this.options[this.currentOption];
      //跳转的时候，用问号传参
      this.$router.push(item.pageUrl + this.searchValue);
    }
  }
};
</script>

<style lang="less" scoped>
//首页容器
.container {
  min-width: 1000px;
  margin: 0 auto;
  position: relative;

  //深入样式，改变轮播图容器的高度
  /deep/ .el-carousel__container {
    height: 700px;
  }
  //轮播图图片
  .banner-image {
    width: 100%;
    height: 100%;
  }

  //2.搜索框
  .banner-content {
    position: absolute;
    left: 50%;
    top: 45%;
    margin-left: -500px;
    border-top: 1px solid transparent;
    width: 1000px;
    z-index: 9;

    //搜索模块
    .search-bar {
      width: 552px;
      margin: 0 auto;
      //tab栏
      .search-tab {
        .active {
          i {
            color: #333;
          }
          //伪元素
          &::after {
            background: #eee;
          }
        }
        //遍历的span
        span {
          position: relative;
          display: block;
          width: 82px;
          height: 36px;
          margin-right: 8px;
          cursor: pointer;
          //文本
          i {
            position: absolute;
            z-index: 2;
            display: block;
            width: 100%;
            height: 100%;
            line-height: 30px;
            text-align: center;
            color: #fff;
          }
          //伪元素
          &::after {
            position: absolute;
            left: 0;
            top: 0;
            display: block;
            content: "";
            width: 100%;
            height: 100%;
            //除了底部，其他边框为透明白色
            border: 1px rgba(225, 255, 255, 0.2) solid;
            border-bottom: none;
            //转换：scale：缩放     perspective：透视   retateX：3D旋转
            transform: scale(1.1, 1.3) perspective(0.7em) rotateX(2.2deg);
            //对称中心
            transform-origin: left bottom;
            background: rgba(0, 0, 0, 0.5);
            border-radius: 1px 2px 0 0;
            box-sizing: border-box;
          }
        }
      }
      //输入框
      .search-input {
        width: 550px;
        height: 46px;
        background: #fff;
        border-radius: 0 4px 4px 0;
        border: 1px solid rgba(255, 255, 255, 0.2);
        border-top: none;
        box-sizing: unset;
        input {
          flex: 1;
          height: 20px;
          padding: 13px 15px;
          outline: none;
          border: 0;
          font-size: 16px;
        }
        //放大镜图标
        .el-icon-search {
          cursor: pointer;
          font-size: 22px;
          padding: 0 10px;
          font-weight: 700;
        }
      }
    }
  }
}
</style>
